package com.edt.ichannel.api.service.impl;


import cc.edt.iceutils2.string.IceStringUtils;
import cc.edt.iceutils3.date.IceDateOperationUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.entiy.local.OrderChannel;
import com.edt.ichannel.api.entiy.local.OrderCreditKd;
import com.edt.ichannel.api.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class ComputeCommisionKdImpl implements ComputeCommissionKdService {


    @Resource
    private OrderCreditKdService orderCreditKdService;

    @Resource
    private SelectFirstMoneyService selectFirstMoneyService;
    @Resource
    private ComputeService computeService;

    @Resource
    private OrderChannelService orderChannelService;
    @Resource
    private MerchantService merchantService;

    @Override
    public void selectFirstMoneyAndComputCommissionMoney(OrderChannel orderChannel) {

        ActionResult result = selectFirstMoneyService.selectFirstMoneyKd(orderChannel);
        if(result.isSuccess()){
            OrderCreditKd orderCreditKd = (OrderCreditKd) result.getResultList();
            if(orderCreditKd != null){
                if(IceStringUtils.isBlank(orderCreditKd.getChannelId())){
                    orderChannelService.setExpansionTypeAndLevl(orderCreditKd);
                }

                if(orderChannel.getSettletType() != null){
                    if(CommonConstant.SETTLET_TYPE_YJ_STR.equals(orderChannel.getSettletType())){
                        orderCreditKd.setOverState(CommonConstant.FLOW_OVER);
                        orderCreditKd.setRemark("KD 月结订单不计算佣金");
                    }
                }else{

                    Integer settletType = merchantService.selectSettletTypeByExapnsionId(orderCreditKd.getChannelId());
                    if(CommonConstant.STAFF_TYPE_QIANY == orderCreditKd.getChannelType() && settletType != null
                            && settletType.equals(CommonConstant.SETTLET_TYPE_YJ) ){
                        orderCreditKd.setOverState(CommonConstant.FLOW_OVER);
                        orderCreditKd.setRemark("KD 签约月结订单不计算佣金");
                    }
                }
                if(orderCreditKd.getOverState() != CommonConstant.FLOW_OVER){
                    if(IceDateOperationUtils.betweenDays(orderCreditKd.getOrderDate(),orderCreditKd.getFirstDate()) > 60){
                        orderCreditKd.setOverState(CommonConstant.FLOW_OVER);
                        orderCreditKd.setRemark("KD 首充时间比订单时间大了 60天以上");
                    }else{
                        computeService.computCommissionKD(orderCreditKd);
                        if(CommonConstant.IS_MONTH.equals(orderCreditKd.getIsMonth())){
                            orderCreditKd.setRemark("KD按月发匹配成功首次发放");
                        }else{
                            if(orderCreditKd.getCommissionMoney() != null){
                                orderCreditKd.setOverState(CommonConstant.FLOW_OVER);
                                orderCreditKd.setRemark("KD 计算佣金成功");
                            }else{
                                orderCreditKd.setOverState(CommonConstant.FLOW_NOT_OVER);
                                orderCreditKd.setRemark("KD 没有匹配到规则");
                            }
                        }

                    }
                }
                orderCreditKd.setAddtime(new Date());
                orderCreditKdService.saveOrUpdateOrderCreditKd(orderCreditKd);
            }


        }
    }
    @Override
    public void moveOrderCreditKdToOrderCredit() {
        orderCreditKdService.moveOrderCreditKdToOrderCredit();
    }

    @Override
    public void moveOrderCreditKdToOrderCreditByOrderCode(String orderCode) {
        orderCreditKdService.moveOrderCreditKdToOrderCreditByOrderCode(orderCode);
    }

}
